/* 
 * This file contains utility classes and function used in the model.
 */

//function getTypeFromId(typeid, model)
function getTypeFromId(typeid, model)
{
    var type = null;
    if (Number(typeid))
    {
        // Number -> id of a class
        //type = PrimitiveType.boolean;
        type = model.getElementById(typeid);
    }
    else
    {
        // Not number -> primitive type
        // Inverse resolveTypeRef(ref)
        switch (typeid)
        {
            case "PrimitiveTypes.boolean":
                type = PrimitiveType.boolean;
                break;
            case "PrimitiveTypes.string":
                type = PrimitiveType.string;
                break;
            case "PrimitiveTypes.real":
                type = PrimitiveType.real;
                break;
            case "PrimitiveTypes.integer":
                type = PrimitiveType.integer;
                break;
            case "PrimitiveTypes.void":
                type = PrimitiveType.void;
                break;
            default:
            // Create new type?
        }
    }

    return type;
}

// Helper function to resolve ref name
//function resolveTypeRef(ref) {
function resolveTypeRef(ref) {
    switch (ref) {
        case PrimitiveType.boolean.uid:
            return "PrimitiveTypes.boolean";
        case PrimitiveType.integer.uid:
            return "PrimitiveTypes.integer";
        case PrimitiveType.real.uid:
            return "PrimitiveTypes.real";
        case PrimitiveType.string.uid:
            return "PrimitiveTypes.string";
        case PrimitiveType.void.uid:
            return "PrimitiveTypes.void";
        default:
            return ref;
    }
}

// Helper function to resolve visibility
//function resolveVisibility(vis) {
function convertVisibilityModelToXml(vis) {
    switch (vis) {
        case "+":
            return "public";
        case "#":
            return "protected";
        case "-":
            return "private";
        default:
            return vis;
    }
}

// Helper function to convert visibility to signs
// function convertVisibility(vis) {
function convertVisibilityXmlToModel(vis) {
    switch (vis) {
        case "public":
            return "+";
        case "protected":
            return "#";
        case "private":
            return "-";
        default:
            return vis;
    }
}

// ======================== GENERATE UID ========================
// 
// Simple function that creates unique identifiers to objects
// Start value is its prototypes nextVal
function UIDGenerator(startVal) {
    this.nextVal = startVal;
}
UIDGenerator.prototype.generateUID = function() {
    return this.nextVal++;
};
UIDGenerator.prototype.setNextValue = function(nextVal) {
    this.nextVal = nextVal;
};
